package com.anjie.powerproject.service.impl;

import com.anjie.powerproject.entity.Point;
import com.anjie.powerproject.entity.TaskConfig;
import com.anjie.powerproject.repository.PointRepository;
import com.anjie.powerproject.repository.TaskConfigRepository;
import com.anjie.powerproject.service.PointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PointServiceImpl implements PointService {
    @Autowired
    PointRepository pointRepository;
    @Autowired
    TaskConfigRepository taskConfigRepository;

    @Override
    public List<Point> getPointList() {
        return pointRepository.findAll();
    }

    @Override
    public Point getPointById(Integer id) {
        return pointRepository.getOne(id);
    }

    @Override
    public boolean deletePointById(Integer id){
        Point point = pointRepository.findById(id).orElse(null);
        if(point != null){
//            List<TaskConfig> taskConfigList=taskConfigRepository.getByDevCode(point.getDevCode());
            List<TaskConfig> taskConfigList=taskConfigRepository.getByPointId(id);
            if(taskConfigList.size()>0) {
                taskConfigRepository.deleteByPointId(id);
            }
            pointRepository.deleteById(id);
            return true;
        }
        return false;
    }

    @Override
    public Integer updatePoint(String pointName,String devCode ,Integer id){
        Point point=pointRepository.getOne(id);
        if(point.getDevCode().equals(devCode)){//相同为ture，则不用同步修改对应的taskConfig
            pointRepository.updatePoint(pointName,devCode,id);
            return 1;
        }
        else if(pointRepository.updatePoint(pointName,devCode,id)==1) {//不相同，要同步修改对应的taskConfig
            pointRepository.updatePoint(pointName,devCode,id);
            List<TaskConfig> taskConfigList=taskConfigRepository.getByDevCode(point.getDevCode());
            for (int i=0;i<taskConfigList.size();i++){
                taskConfigList.get(i).setDevCode(devCode);
                taskConfigRepository.saveAndFlush(taskConfigList.get(i));
            }
            return 1;
        }
        else
            return 0;
    }

    /**
     * 根据参数模糊查询
     * @param pointName
     * @param devCode
     * @return
     */
    @Override
    public List<Point> getPointListByConditions(String pointName,String devCode) {
        return pointRepository.getPointListByConditions(pointName,devCode);
    }

    @Override
    public Point add(Point point) {
        Point point1=pointRepository.saveAndFlush(point);
        if(null==point1)
            return null;
        else
            return point1;
    }

    /**
     * 查询数据库中是否有point的pointName或者devCode和当前要存入point重复，重复返回1，否则返回0
     * @param pointName
     * @param devCode
     * @return
     */
    @Override
    public Integer pointIsExist(String pointName, String devCode) {
        return pointRepository.pointIsExist(pointName,devCode);
    }

    /**
     * 查询数据库中是否有point的devCode和当前要point重复，重复返回1，否则返回0
     * @param devCode
     * @return
     */
    @Override
    public Integer pointDevCodeIsExist(String devCode) {
        return pointRepository.pointDevCodeIsExist(devCode);
    }

    /**
     * 查询数据库中是否有point的pointName和当前要point重复，重复返回1，否则返回0
     * @param pointName
     * @return
     */
    @Override
    public Integer pointNameIsExist(String pointName) {
        return pointRepository.pointNameIsExist(pointName);
    }


    /**
     * 返回所有 point 的 devCode
     * @return
     */
    @Override
    public List<String> getAllPointDevCode() {
        return pointRepository.getAllPointDevCode();
    }

    /**
     * 模糊搜索devCode
     * @return
     */
    @Override
    public List<String> fuzzySelectPointDevCode(String devCode) {
        return pointRepository.fuzzySelectPointDevCode(devCode);
    }


    /**
     * 通过devCode获取point
     * @return
     */
    @Override
    public Point getPointByDevCode(String devCode){
        return pointRepository.getPointByDevCode(devCode);
    }

}
